<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Picture extends CI_Controller {

	public function index($start = 0){
	   $this->load->model('Pictures', null, true);
       
       $this->load->library('pagination');
       $cofigPagination = $this->config->item('pagination');

        $config['base_url'] = base_url().'picture/index';
        $config['total_rows'] = $this->Pictures->getCount();
        $config['per_page'] = $cofigPagination['number_row']; 
        $this->pagination->initialize($config); 

        
       $data['Pictures'] = $this->Pictures->getList($start, $cofigPagination['number_row']);
	   $this->load->view('picture/index', $data);
	}
    
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($pictureId){
         $this->load->model('Pictures', null, true);
         if($this->Pictures->delete($pictureId)){
            
            redirect(base_url().'picture.html');
         }
    }
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function add(){
        $res = false;
        $this->load->model('Pictures', null, true);
        $data = array();
        if($this->input->post()){
             $data = $this->input->post();             
             if($_FILES['Image']['name'] != ''){
                $resUpload = $this->_uploadFile('Image');
                if(!is_null($resUpload)){ 
                    $data['PictureLink'] = $resUpload['file_name'];
                    if($this->Pictures->add($data)){
                        redirect(base_url().'picture.html');
                    }
                }          
             }else{
                $data['message_error'] = "Vui lòng chọn hình upload.";
             }  
        }
        
        $this->load->view('picture/add', $data);       
    }
    
    /**
     * upload file 
     * @author tien.nguyen
     */
    private function _uploadFile($fileName){
       try{
           $config['upload_path'] = $this->config->item('base_upload').'/pictures';;
           $config['allowed_types'] = 'gif|png|jpg';
           $config['max_size'] = '1000';
           $config['max_width'] =  '3000';
           $config['max_height'] = '3000';
           
           $this->load->library('upload', $config);
           if(!$this->upload->do_upload($fileName)){
                $this->session->set_flashdata('message', $this->upload->error_msg);
                return null;
           }else{
                return $this->upload->data();
           }
       }catch(exception $e){
         echo $e->__toString(); die;
       }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */